/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import CRUD.ThreadHome;
import Utility.ThreadPollsUtility;
import beans.UserType;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import xmlMapping.Member;
import xmlMapping.Thread;
import xmlMapping.Threadpollsquestions;

/**
 *
 * @author ABDELMOHSEN
 */
public class DeleteVote extends org.apache.struts.action.Action {
    
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        ThreadHome thHome=new ThreadHome();
        Thread th=thHome.findById(Integer.parseInt(request.getParameter("threadid")));
        Member m=th.getMember();
        UserType type=(UserType)request.getSession().getAttribute("type");
        if(type==null||(type.equals(UserType.mem)&&m.getMemberId()!=(Integer)request.getSession().getAttribute("id"))) return mapping.findForward("login");
        Member comander=new Member();
        comander.setMemberId((Integer)request.getSession().getAttribute("id"));
        Threadpollsquestions q=(Threadpollsquestions) th.getThreadpollsquestionses().iterator().next();
        new ThreadPollsUtility().deleteThreadPolls(q.getPollsId(), comander);
        request.setAttribute("threadid", th.getThreadId());
        return mapping.findForward("done");
    }
}
